<script setup lang="ts">
import { ref, watch, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { FormInstance, FormRules } from 'element-plus'
import { postLoginAPI, postRegisterAPI } from '@/services/auth/auth'
import {
  LoginParams,
  RegisterParams,
  LoginResponseData,
} from '@/services/auth/type'
import { useUserStore } from '@/stores'
const $router = useRouter()
const userStore = useUserStore()
const isLogin = ref<boolean>(true)
// 获取到遮罩元素
const maskRef = ref<HTMLDivElement | null>(null)

// 获取注册表单元素
const registerFormRef = ref<FormInstance>()

// 注册表单数据
const registerForm = reactive<RegisterParams>({
  username: '',
  gender: '',
  phone: '',
  password: '',
})

// 定义注册表单规则
const registerRules = reactive<FormRules<RegisterParams>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z\u4e00-\u9fa5][a-zA-Z0-9\u4e00-\u9fa5]{1,}$/,
      message: '用户名为不能以数字开头的起码两位字符',
      trigger: 'blur',
    },
  ],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    {
      pattern:
        /^(13\d|14[5-9]|15[0-3,5-9]|16[6]|17[0,3,5-8]|18\d|19[8,9])\d{8}$/,
      message: '手机号格式不正确',
      trigger: 'blur',
    },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^.{6,}$/,
      message: '密码至少六位',
      trigger: 'blur',
    },
  ],
})

// 确认注册
const confimRegister = async () => {
  await registerFormRef.value?.validate()
  await postRegisterAPI(registerForm)
  ElMessage({
    type: 'success',
    message: '注册成功',
  })
  isLogin.value = true
}

// 获取到登录表单元素
const loginFormRef = ref<FormInstance>()

// 登录表单数据
const loginForm = reactive<LoginParams>({
  username: '',
  password: '',
})

// 定义登录表单规则
const loginRules = reactive<FormRules<LoginParams>>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

// 确认登录
const confimLogin = async () => {
  await loginFormRef.value?.validate()
  const { data: res }: LoginResponseData = await postLoginAPI(loginForm)
  userStore.setToken(res.access_token)
  ElMessage.success('登录成功')
  setTimeout(() => {
    $router.push({ path: '/home' })
  }, 500)
}

watch(
  () => isLogin.value,
  (newVal) => {
    if (newVal) {
      maskRef.value!.style.left = '5%'
    } else {
      const maskWidth = maskRef.value!.offsetWidth
      maskRef.value!.style.left = `calc(95% - ${maskWidth}px)`
    }
  }
)

const options = [
  {
    value: '0',
    label: '男',
  },
  {
    value: '1',
    label: '女',
  },
  {
    value: '2',
    label: '保密',
  },
]
</script>
<template>
  <div class="register_bg">
    <div class="register_box">
      <!-- 遮罩 -->
      <div class="mask_box" ref="maskRef">
        <div class="form" v-if="isLogin">
          <p>LOGIN</p>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules">
            <el-form-item prop="username">
              <el-input
                placeholder="用户名"
                v-model="loginForm.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                placeholder="密码"
                v-model="loginForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="confimBtn" @click="confimLogin">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="form" v-else>
          <p>REGISTER</p>
          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
          >
            <el-form-item prop="username">
              <el-input
                placeholder="用户名"
                v-model="registerForm.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="gender">
              <el-select
                class="m-2"
                placeholder="性别"
                size="large"
                v-model="registerForm.gender"
              >
                <el-option label="请选择性别" :value="''" />
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input
                placeholder="联系方式"
                v-model="registerForm.phone"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                placeholder="密码"
                v-model="registerForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="confimBtn" @click="confimRegister"
                >注册</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
      <!-- 内容盒子 -->
      <div class="main_box">
        <div class="content">
          <div class="img_box">
            <img src="../../../assets/打视频电话.png" alt="" />
          </div>
          <p>已有账号?</p>
          <el-button type="primary" plain @click="isLogin = !isLogin"
            >去登录</el-button
          >
        </div>
        <div class="content">
          <div class="img_box">
            <img src="../../../assets/阅读文献.png" alt="" />
          </div>
          <p>还没有账号?</p>
          <el-button type="primary" plain @click="isLogin = !isLogin"
            >去注册</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.register_bg {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, #fff, #cbe5ff);
  display: flex;
  justify-content: center;
  align-items: center;
  .register_box {
    position: relative;
    width: 50%;
    height: 65%;
    display: flex;
    align-items: center;
    .mask_box {
      position: absolute;
      z-index: 2;
      top: 0;
      left: 5%;
      height: 100%;
      width: 45%;
      background: #99ccff;
      transition: all 0.5s;
      display: flex;
      align-items: center;
      justify-content: center;
      .form {
        width: 80%;
        height: 70%;
        p {
          text-align: center;
          color: #fff;
          font-size: 30px;
          font-weight: 600;
        }
        .confimBtn {
          width: 100%;
          margin-top: 20px;
        }
      }
    }
    .main_box {
      width: 100%;
      height: 80%;
      margin: auto 0;
      background-color: #fff;
      display: flex;
      .content {
        width: 50%;
        display: flex;
        flex-direction: column;
        align-items: center;
        p {
          text-align: center;
          color: #999;
          font-size: 16px;
          font-weight: 500;
          margin-bottom: 10px;
        }
        .img_box {
          width: 80%;
          height: 80%;
          img {
            width: 100%;
            height: 100%;
            background-size: cover;
          }
        }
      }
    }
  }
}

:deep(.el-form) {
  margin-top: 20px;
  .el-form-item {
    margin-top: 20px;
  }
}

:deep(.el-input__wrapper) {
  width: 100%;
  background-color: transparent;
  border: none;
  box-shadow: none;
  color: #fff;
  border-bottom: 1px solid rgb(247, 239, 249) !important;
}
:deep(.el-select .el-input.is-focus .el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.el-input__wrapper.is-focus) {
  box-shadow: none !important;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: none !important;
}

:deep(.el-input__inner::placeholder) {
  color: #fff;
}

:deep(.el-select .el-input .el-select__caret.el-icon) {
  color: #fff;
}
:deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: none;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__inner) {
  color: #fff;
}
</style>
